/*
 *  UI_Tab.cpp
 *
 *	Date: 2011-8-8
 *  Author: linshenqi
 */

#include "stdafx.h"
#include "./UI_Frame.h"
#include "Resource.h"
#include "./SSCOM.h"
#include "./Parameter.h"
#include "./Configer.h"

extern PARAMETER theParameter[2];
extern SYSPARAM theSysParam;
extern Configer theCfg;
#define PADD		16
#define SUB_PADD	16
extern SSCOM theSSCOM;
extern CDC *g_pDC;
extern CDC g_MemDC;
extern int theSelection;

UI_Tab::UI_Tab(const CRect *pRect, const GRAPHICPROP *pProp)
	: GraphicBase(pRect, pProp)
	, m_nSel(0)
	, m_pEdit(NULL)
	, m_nEdit(-1)
	, m_bEdit(false)
{
	this->m_Str[0] = "Conference";
	this->m_Str[1] = "Presentation";

	m_pEdit = new CEdit;

	m_ft[0] = RGB(255, 255, 255);
	m_ft[1] = COLOR_FONT_OUT;

	m_lf.lfCharSet = DEFAULT_CHARSET;
	m_lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	m_lf.lfEscapement = 0;
	strcpy(m_lf.lfFaceName, FONT_CAPTION);
	m_lf.lfHeight = 24;
	m_lf.lfItalic = true;
	m_lf.lfOrientation = 0;
	m_lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	m_lf.lfPitchAndFamily = DEFAULT_PITCH;
	m_lf.lfQuality = DEFAULT_QUALITY;
	m_lf.lfStrikeOut = false;
	m_lf.lfUnderline = false;
	m_lf.lfWidth = 0;
	m_lf.lfWeight = 0;
}

bool UI_Tab::OnPaint(CPaintDC *pDC, CDC *pMemDC)
{
	GRAPHICPROP prop;
	prop.clrFill = TAB_BG;
	prop.clrFrame = TAB_BG;
	//this->DrawRect(this->m_pRect, &prop, pDC, pDC);

	prop.clrFill = RGB(0, 0, 0);
	prop.clrFrame = RGB(0, 0, 0);
	this->DrawPolygon(this->Tab1, 4, &prop, pDC, pDC);
	this->DrawPolygon(this->Tab2, 4, &prop, pDC, pDC);

	this->DrawText(&m_lf, &m_rc[0], this->m_Str[0], m_ft[0], pDC, pDC);

	this->DrawText(&m_lf, &m_rc[1], this->m_Str[1], m_ft[1], pDC, pDC);

	return true;
}

void UI_Tab::Redraw()
{
	GRAPHICPROP prop;
	prop.clrFill = TAB_BG;
	prop.clrFrame = TAB_BG;
	//this->DrawRect(this->m_pRect, &prop, pDC, pDC);

	prop.clrFill = RGB(0, 0, 0);
	prop.clrFrame = RGB(0, 0, 0);
	this->DrawPolygon(this->Tab1, 4, &prop, g_pDC, g_pDC);
	this->DrawPolygon(this->Tab2, 4, &prop, g_pDC, g_pDC);

	this->DrawText(&m_lf, &m_rc[0], this->m_Str[0], m_ft[0], g_pDC, g_pDC);

	this->DrawText(&m_lf, &m_rc[1], this->m_Str[1], m_ft[1], g_pDC, g_pDC);
}

bool UI_Tab::OnLButtonDown(UINT nFlags, CPoint point)
{
	if(this->m_bEdit)
	{
		CRect rc;
		m_pEdit->GetWindowRect(&rc);
		if(!rc.PtInRect(point))
		{
			this->EndSetText();
		}
		return true;
	}

	if(!this->m_pRect->PtInRect(point))
		return false;

	if(this->PtInPolygon(point, Tab1, 4))
	{
		Select(1);
	}
	else if(this->PtInPolygon(point, Tab2, 4))
	{
		Select(2);
	}

	return GraphicBase::OnLButtonDown(nFlags, point);
}

void UI_Tab::SetRect(const CRect *pRect)
{
	GraphicBase::SetRect(pRect);
	this->Tab1[0].x = this->m_pRect->left + PADD;
	this->Tab1[0].y = this->m_pRect->top;
	this->Tab1[1].x = this->m_pRect->CenterPoint().x - PADD;
	this->Tab1[1].y = this->m_pRect->top;
	this->Tab1[2].x = this->m_pRect->CenterPoint().x;
	this->Tab1[2].y = this->m_pRect->bottom;
	this->Tab1[3].x = this->m_pRect->left;
	this->Tab1[3].y = this->m_pRect->bottom;
	m_rc[0].SetRect(this->m_pRect->left, 
					this->m_pRect->top, 
					this->m_pRect->CenterPoint().x, 
					this->m_pRect->top + 4);

	this->Tab2[0].x = this->m_pRect->CenterPoint().x + PADD;
	this->Tab2[0].y = this->m_pRect->top;
	this->Tab2[1].x = this->m_pRect->right - PADD;
	this->Tab2[1].y = this->m_pRect->top;
	this->Tab2[2].x = this->m_pRect->right - PADD * 2 / 5;
	this->Tab2[2].y = this->m_pRect->bottom - this->m_pRect->Height() * 2 / 5;
	this->Tab2[3].x = this->m_pRect->CenterPoint().x + PADD * 2 / 5;
	this->Tab2[3].y = this->m_pRect->bottom - this->m_pRect->Height() * 2 / 5;
	m_rc[1].SetRect(this->m_pRect->CenterPoint().x, 
					   this->m_pRect->top, 
					   this->m_pRect->right, 
					   this->m_pRect->top + 1);
}

void UI_Tab::Switch(int nIndex)
{
	if(nIndex == 1)
	{
		::theSelection = 0;
		this->Tab1[0].x = this->m_pRect->left + PADD;
		this->Tab1[0].y = this->m_pRect->top;
		this->Tab1[1].x = this->m_pRect->CenterPoint().x - PADD;
		this->Tab1[1].y = this->m_pRect->top;
		this->Tab1[2].x = this->m_pRect->CenterPoint().x;
		this->Tab1[2].y = this->m_pRect->bottom;
		this->Tab1[3].x = this->m_pRect->left;
		this->Tab1[3].y = this->m_pRect->bottom;
		m_rc[0].SetRect(this->m_pRect->left, 
					   this->m_pRect->top, 
					   this->m_pRect->CenterPoint().x, 
					   this->m_pRect->top + 4);

		this->Tab2[0].x = this->m_pRect->CenterPoint().x + PADD;
		this->Tab2[0].y = this->m_pRect->top;
		this->Tab2[1].x = this->m_pRect->right - PADD;
		this->Tab2[1].y = this->m_pRect->top;
		this->Tab2[2].x = this->m_pRect->right - PADD * 2 / 5;
		this->Tab2[2].y = this->m_pRect->bottom - this->m_pRect->Height() * 2 / 5;
		this->Tab2[3].x = this->m_pRect->CenterPoint().x + PADD * 2 / 5;
		this->Tab2[3].y = this->m_pRect->bottom - this->m_pRect->Height() * 2 / 5;
		m_rc[1].SetRect(this->m_pRect->CenterPoint().x, 
					   this->m_pRect->top, 
					   this->m_pRect->right, 
					   this->m_pRect->top + 1);

		m_nSel = 0;

		if(!::theSysParam.theParameter[0].StatusPanel.bStmt)
		{
			this->m_ft[0] = RGB(255, 255, 255);
		}
		else
		{
			this->m_ft[0] = COLOR_FORE_MOUSEIN;
		}

		
		if(::theSysParam.theParameter[1].StatusPanel.bStmt)
		{
			this->m_ft[1] = COLOR_FORE_MOUSEIN;
		}
		else
		{
			this->m_ft[1] = COLOR_FONT_OUT;
		}
	}
	else
	{
		::theSelection = 1;
		this->Tab1[0].x = this->m_pRect->left + PADD;
		this->Tab1[0].y = this->m_pRect->top;
		this->Tab1[1].x = this->m_pRect->CenterPoint().x - PADD;
		this->Tab1[1].y = this->m_pRect->top;
		this->Tab1[2].x = this->m_pRect->CenterPoint().x - PADD * 2 / 5;
		this->Tab1[2].y = this->m_pRect->bottom - this->m_pRect->Height() * 2 / 5;
		this->Tab1[3].x = this->m_pRect->left + PADD * 2 / 5;
		this->Tab1[3].y = this->m_pRect->bottom - this->m_pRect->Height() * 2 / 5;
		m_rc[0].SetRect(this->m_pRect->left, 
					   this->m_pRect->top, 
					   this->m_pRect->CenterPoint().x, 
					   this->m_pRect->top + 1);

		this->Tab2[0].x = this->m_pRect->CenterPoint().x + PADD;
		this->Tab2[0].y = this->m_pRect->top;
		this->Tab2[1].x = this->m_pRect->right - PADD;
		this->Tab2[1].y = this->m_pRect->top;
		this->Tab2[2].x = this->m_pRect->right;
		this->Tab2[2].y = this->m_pRect->bottom;
		this->Tab2[3].x = this->m_pRect->CenterPoint().x;
		this->Tab2[3].y = this->m_pRect->bottom;
		m_rc[1].SetRect(this->m_pRect->CenterPoint().x, 
					   this->m_pRect->top, 
					   this->m_pRect->right, 
					   this->m_pRect->top + 4);

		m_nSel = 1;

		if(!::theSysParam.theParameter[1].StatusPanel.bStmt)
		{
			this->m_ft[1] = RGB(255, 255, 255);
		}
		else
		{
			this->m_ft[1] = COLOR_FORE_MOUSEIN;
		}

		if(::theSysParam.theParameter[0].StatusPanel.bStmt)
		{
			this->m_ft[0] = COLOR_FORE_MOUSEIN;
		}
		else
		{
			this->m_ft[0] = COLOR_FONT_OUT;
		}
	}

	if(this->m_pParent)
		this->m_pParent->Redraw();
}

void UI_Tab::Select(int nIndex)
{
	if((nIndex == m_nSel + 1) || m_bEdit)
		return;

	Switch(nIndex);
	::theCfg.Load(nIndex - 1, false);

	::theSysParam.theParameter[0].TitleBar.Tab.nSwitch = ::theSelection;
	::theSysParam.theParameter[1].TitleBar.Tab.nSwitch = ::theSelection;

	//::theCfg.m_bChanged = false;
}

void UI_Tab::SetID(int id)
{
	GraphicBase::SetID(id);
}

void UI_Tab::SetBaseWindow(CWnd *pWnd)
{
	GraphicBase::SetBaseWindow(pWnd);

	CRect rc;
	this->m_pEdit->Create(WS_CHILD | WS_VISIBLE | ES_CENTER,
						  rc,
						  pWnd,
						  IDD_TAB_EDIT);

	this->m_pEdit->ShowWindow(SW_HIDE);
	this->m_pEdit->EnableWindow(false);
}

void UI_Tab::StartSetText(CString& str, int n)
{
	if(!this->m_pEdit)
		return;

	if(m_bEdit)
		return;

	m_bEdit = true;
	m_nEdit = n;
	CRect rc;
	if(n == 0)
	{
		rc.SetRect(this->m_pRect->CenterPoint().x - this->m_pRect->Width() / 4 - 96,
				   this->m_pRect->CenterPoint().y - 12,
				   this->m_pRect->CenterPoint().x - this->m_pRect->Width() / 4 + 96,
				   this->m_pRect->CenterPoint().y + 8);
	}
	else
	{
		rc.SetRect(this->m_pRect->CenterPoint().x + this->m_pRect->Width() / 4 - 96,
				   this->m_pRect->CenterPoint().y - 12,
				   this->m_pRect->CenterPoint().x + this->m_pRect->Width() / 4 + 96,
				   this->m_pRect->CenterPoint().y + 8);
	}

	m_pEdit->MoveWindow(&rc);
	m_pEdit->SetWindowText(str.GetBuffer(str.GetLength()));
	m_pEdit->ShowWindow(SW_SHOW);
	m_pEdit->SetFocus();
	this->m_pEdit->EnableWindow(true);
}

void UI_Tab::EndSetText()
{
	::theCfg.m_bChanged = true;
	m_pEdit->ShowWindow(SW_HIDE);
	this->m_pEdit->GetWindowText(this->m_Str[m_nEdit]);
	::theSysParam.theParameter[::theSelection].TitleBar.Tab.sName = this->m_Str[m_nEdit];
	this->Redraw();

	m_nEdit = -1;
	m_bEdit = false;
}

void UI_Tab::CancelSetText()
{
	m_pEdit->ShowWindow(SW_HIDE);
	this->Redraw();
	m_nEdit = -1;
	m_bEdit = false;
}

bool UI_Tab::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	if(!this->m_pRect->PtInRect(point))
		return false;

	if(this->PtInPolygon(point, Tab1, 4))
	{
		if(m_nSel == 0)
		{
			StartSetText(this->m_Str[0], 0);
		}
	}
	else if(this->PtInPolygon(point, Tab2, 4))
	{
		if(m_nSel == 1)
		{
			StartSetText(this->m_Str[1], 1);
		}
	}

	return GraphicBase::OnLButtonDblClk(nFlags, point);
}